export function tableValueAreaDetail(val) {
  if (val) {
    return val
      .split(",")
      .map((item) => {
        const arr = item.split("_");
        if (arr[0] === "d") {
          return arr[1];
        } else {
          return arr[1];
        }
      })
      .join("");
  }
  return "";
}
export function tableValueArea(val) {
  if (val) {
    return val
      .split(",")
      .map((item) => {
        const arr = item.split("_");
        if (arr[0] === "d") {
          return arr[1];
        } else {
          return arr[1];
        }
      })
      .join("");
  }
  return "";
}

export function deFormatValues(list, fieldMap) {
  const data = {};
  for (let i = 0; i < list.length; i++) {
    const item = list[i];
    const key = `${item.id}_${item.fieldCode || ""}`;
    data[key] = item.content;
    if (fieldMap[key]) {
      if (fieldMap[key].type === "area_detail") {
        if (item.content) {
          data[key] = item.content
            .split(",")
            .map((item) => {
              const arr = item.split("_");
              if (arr[0] === "d") {
                data[`${key}_detail`] = arr[1];
                return false;
              } else {
                return parseInt(arr[0]);
              }
            })
            .filter((item) => item);
        }
      } else if (fieldMap[key].type === "area") {
        if (item.content) {
          data[key] = item.content
            .split(",")
            .map((item) => {
              const arr = item.split("_");
              return parseInt(arr[0]);
            })
            .filter((item) => item);
        }
      } else if (fieldMap[key].type === "date") {
        if (item.content) {
          if (item.content.includes("~")) {
            data[key] = item.content.split("~");
          } else {
            data[key] = item.content;
          }
        }
      }
    }
  }
  return data;
}

export function formatValues(res, fieldMap, areaSourceMap) {
  const list = [];
  const fields = Object.values(fieldMap);
  for (let i = 0; i < fields.length; i++) {
    const field = fields[i];
    const key = field.model;
    if (res[key]) {
      const keyArr = key.split("_");
      let value = res[key];
      if (field.type === "area_detail") {
        const areaArr = [];
        for (let j = 0; j < res[key].length; j++) {
          const aId = res[key][j];
          areaArr.push(`${aId}_${areaSourceMap[aId]["name"]}`);
        }
        if (res[`${key}_detail`]) {
          areaArr.push(`d_${res[`${key}_detail`]}`);
        }
        value = areaArr.join(",");
      } else if (field.type === "area") {
        const areaArr = [];
        for (let j = 0; j < res[key].length; j++) {
          const aId = res[key][j];
          areaArr.push(`${aId}_${areaSourceMap[aId]["name"]}`);
        }
        value = areaArr.join(",");
      } else if (field.type === "date") {
        if (Object.prototype.toString.call(value) === "[object Array]") {
          const dateArr = [];
          for (let j = 0; j < res[key].length; j++) {
            const date = res[key][j];
            dateArr.push(date.format("YYYY-MM-DD"));
          }
          value = dateArr.join("~");
        } else {
          value = res[key];
        }
      } else if (field.type === "upload") {
        if (Object.prototype.toString.call(value) === "[object Array]") {
          value = value.map((item) => item.url).join(",");
        }
      }
      list.push({
        fieldId: parseInt(keyArr[0]),
        enName: keyArr[1],
        content: value
      });
    }
  }
  return list;
}
